package cc.vv.lkbasecomponent.base.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * @author Zml
 * @date 2018/3/13.
 */
public class LKListDialog extends Dialog implements AdapterView.OnItemClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private TextView mTitle;
    private View mDividerLine;
    private ListView mListView;
    private List<ListObj> mData;
    private OnItemClick mItemClick;

    public LKListDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
        initView(context);
    }

    /**
     * 初始化控件
     *
     * @param context
     */
    private void initView(Context context) {
        mTitle = new TextView(context);
        mTitle.setTextSize(20);
        mTitle.setTextColor(Color.BLACK);
        mTitle.setPadding(dp2px(25), dp2px(15), 0, dp2px(15));
        mTitle.setVisibility(View.GONE);
        mDividerLine = new View(mContext);
        mDividerLine.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, dp2px(1)));
        mDividerLine.setBackgroundColor(Color.BLACK);
        mDividerLine.setVisibility(View.GONE);
        mListView = new ListView(context);
        mListView.setOnItemClickListener(this);
        mListView.setHeaderDividersEnabled(true);
        mLinearLayout = new LinearLayout(context);
        mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
        mLinearLayout.addView(mTitle);
        mLinearLayout.addView(mDividerLine);
        mLinearLayout.addView(mListView);
        this.addContentView(mLinearLayout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    /**
     * 设置Dialog标题
     *
     * @param title
     * @return
     */
    public LKListDialog setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTitle.setVisibility(View.VISIBLE);
            mDividerLine.setVisibility(View.VISIBLE);
            mTitle.setText(title);
        }
        return this;
    }

    /**
     * 设置 Title 文件和分割线颜色
     *
     * @param color
     * @return
     */
    public LKListDialog setTitleColor(int color) {
        mTitle.setTextColor(color);
        mDividerLine.setBackgroundColor(color);
        return this;
    }

    /**
     * 设置 dialog 背景颜色
     *
     * @param color
     * @return
     */
    public LKListDialog setBackgroundColor(int color) {
        mLinearLayout.setBackgroundColor(color);
        return this;
    }

    /**
     * 设置列表数据
     *
     * @param list
     * @return
     */
    public LKListDialog setData(List<ListObj> list) {
        this.mData = list;
        if (null != mData && mData.size() > 0) {
            mListView.setAdapter(mAdapter);
        }
        return this;
    }

    /**
     * 设置列表点击回调
     *
     * @param itemClick
     * @return
     */
    public LKListDialog listItemClick(OnItemClick itemClick) {
        this.mItemClick = itemClick;
        return this;
    }

    /**
     * 设置是否点击dialog以外的区域
     *
     * @param cancel
     * @return
     */
    public LKListDialog canceledOnTouchOutside(boolean cancel) {
        this.setCanceledOnTouchOutside(cancel);
        return this;
    }

    /**
     * show Dialog
     */
    @Override
    public void show() {
        super.show();
    }

    /**
     * 列表数据适配器
     */
    private BaseAdapter mAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int i) {
            return mData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView textView = new TextView(mContext);
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#333333"));
            textView.setPadding(dp2px(28), dp2px(13), 0, dp2px(13));
            textView.setText(mData.get(i).getContent());
            if (null == view) {
                view = textView;
            }
            return view;
        }
    };


    /**
     * dp转换成px
     */
    private int dp2px(float dpValue) {
        float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * item 点击事件处理
     *
     * @param adapterView
     * @param view
     * @param i
     * @param l
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        mItemClick.onItemClick(view, mData.get(i), i);
        dismiss();
    }

    /**
     * 自定义列表点击事件返回
     */
    public interface OnItemClick {
        /**
         * item 点击事件处理
         *
         * @param view
         * @param listObj
         * @param position
         */
        void onItemClick(View view, ListObj listObj, int position);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    /**
     * list 对应的数据类
     */
    public static class ListObj {
        private String code;
        private String content;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public ListObj() {
            super();
        }

        public ListObj(String code, String content) {
            this.code = code;
            this.content = content;
        }
    }

}
